<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h2>场馆预约管理系统</h2>
        <p>管理员登录</p>
      </div>
      
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        @submit.native.prevent="handleLogin">
        
        <el-form-item prop="userName">
          <el-input
            v-model="loginForm.userName"
            placeholder="请输入用户名"
            prefix-icon="el-icon-user"
            size="large"
            clearable>
          </el-input>
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            size="large"
            show-password
            @keyup.enter.native="handleLogin">
          </el-input>
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="primary"
            size="large"
            class="login-button"
            :loading="loading"
            @click="handleLogin">
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
      </el-form>
      
      <div class="login-footer">
        <p>© 2023 场馆预约管理系统</p>
      </div>
    </div>
  </div>
</template>

<script>
import { authAPI } from '../api/auth.js'

export default {
  name: 'Login',
  data() {
    return {
      loading: false,
      loginForm: {
        userName: '',
        password: ''
      },
      loginRules: {
        userName: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 20, message: '用户名长度在 2 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async handleLogin() {
      try {
        await this.$refs.loginForm.validate()
        this.loading = true
        
        const result = await authAPI.login(this.loginForm)
        
        if (result.success && result.data && result.data.token) {
          // 保存token到localStorage
          localStorage.setItem('admin_token', result.data.token)
          localStorage.setItem('admin_user', this.loginForm.userName)
          
          this.$message.success('登录成功')
          
          // 通知父组件登录成功
          this.$emit('login-success')
        } else {
          this.$message.error(result.message || '登录失败')
        }
      } catch (error) {
        if (error !== false) {
          this.$message.error('登录失败: ' + (error.message || '网络错误'))
        }
      } finally {
        this.loading = false
      }
    }
  },
  mounted() {
    // 检查是否已经登录
    const token = localStorage.getItem('admin_token')
    if (token) {
      this.$emit('login-success')
    }
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
}

.login-box {
  background: white;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  padding: 40px;
  width: 100%;
  max-width: 400px;
  backdrop-filter: blur(10px);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  color: #303133;
  font-size: 24px;
  font-weight: 600;
  margin-bottom: 8px;
}

.login-header p {
  color: #909399;
  font-size: 14px;
  margin: 0;
}

.login-form {
  margin-bottom: 20px;
}

.login-form .el-form-item {
  margin-bottom: 20px;
}

.login-button {
  width: 100%;
  height: 44px;
  font-size: 16px;
  font-weight: 500;
  border-radius: 8px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  transition: all 0.3s ease;
}

.login-button:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}

.login-footer {
  text-align: center;
  color: #C0C4CC;
  font-size: 12px;
}

.login-footer p {
  margin: 0;
}

/* Element UI 样式覆盖 */
.login-form .el-input__inner {
  height: 44px;
  border-radius: 8px;
  border: 1px solid #DCDFE6;
  font-size: 14px;
  transition: all 0.3s ease;
}

.login-form .el-input__inner:focus {
  border-color: #667eea;
  box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.2);
}

.login-form .el-input__prefix {
  left: 12px;
}

.login-form .el-input--prefix .el-input__inner {
  padding-left: 40px;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-box {
    padding: 30px 20px;
    margin: 0 10px;
  }
  
  .login-header h2 {
    font-size: 20px;
  }
}
</style>